package com.mall.manager.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.mall.untils.UploadImgOrRecorderUtil;
/**
 * 图片上传
 * @author Administrator
 *
 */
@Controller
public class CommonUpload {
    Logger logger = LoggerFactory.getLogger(CommonUpload.class);
	 /**
     * 图片上传
     * @return
     */
    @RequestMapping(value = "/ueditor_web_upload_Img")
    @ResponseBody
    public String ueditor_web_upload_Img(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> map=new HashMap<String,Object>();
    	try {
    		response.setContentType("text/html;charset=UTF-8");
    		MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
    		MultipartFile file =multipartRequest.getFile("upfile");
    		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));  
    		logger.info(file.getOriginalFilename()+"======"+suffix);
    		String new_name =  System.currentTimeMillis()+UUID.randomUUID().toString()+ suffix;
    		String url=UploadImgOrRecorderUtil.createImgFileByName(file.getInputStream(), file.getSize(),"operating_platform/img_audio/"+new_name);
    		map.put("original", new_name);
    		map.put("title", new_name);
    		map.put("state", "SUCCESS");
    		map.put("url", url);
		} catch (Exception e) {
			map.put("original", "");
    		map.put("title", "");
    		map.put("state", "FAIL");
    		map.put("url", "");
    		e.printStackTrace();
		}
    	JSONObject fromObject = JSONObject.fromObject(map);
		logger.info("图片上传======"+fromObject.toString());
    	return fromObject.toString();
    }
    
    /**
     * 图片上传
     * @return
     */
    @RequestMapping(value = "/ueditor_web_upload_vedio")
    @ResponseBody
    public String ueditor_web_upload_vedio(HttpServletRequest request,HttpServletResponse response){
    	Map<String, Object> map=new HashMap<String,Object>();
    	try {
    		response.setContentType("text/html;charset=UTF-8");
    		MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
    		MultipartFile file =multipartRequest.getFile("upfile");
    		String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));  
    		logger.info(file.getOriginalFilename()+"======"+suffix);
    		String new_name =  System.currentTimeMillis()+UUID.randomUUID().toString()+ suffix;
    		String url=UploadImgOrRecorderUtil.createImgFileByName_vedio(file.getInputStream(), file.getSize(),"operating_platform/img_audio/"+new_name);
    		map.put("original", new_name);
    		map.put("title", new_name);
    		map.put("state", "SUCCESS");
    		map.put("url", url);
		} catch (Exception e) {
			map.put("original", "");
    		map.put("title", "");
    		map.put("state", "FAIL");
    		map.put("url", "");
    		e.printStackTrace();
		}
    	JSONObject fromObject = JSONObject.fromObject(map);
		logger.info("图片上传======"+fromObject.toString());
    	return fromObject.toString();
    }
    
    /**
     * 图片上传
     * @return
     */
    @RequestMapping(value = "/web_upload_Img")
    @ResponseBody
    public String web_upload_Img(HttpServletRequest request,HttpServletResponse response,  @RequestParam(value="Filedata", required=false) MultipartFile file) throws IOException{
    	String url="";
		if (file != null && file.getSize() != 0) {
			String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));  
    		String new_name =  System.currentTimeMillis()+UUID.randomUUID().toString()+ suffix;
			url = UploadImgOrRecorderUtil.createImgFileByName(file.getInputStream(), file.getSize(), "operating_platform/img_audio/"+new_name);
        }
    	return url;
    }
}
